package com.lingnan.blog.mapper;

import com.lingnan.blog.entity.User;
import com.lingnan.blog.provider.UserProvider;
import org.apache.ibatis.annotations.*;

/**
 * @author HeGuanChao
 */
@Mapper
public interface UserMapper {
    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return 一个用户的信息
     */
    @Select("select * from user where id = #{id} ")
    @Results({
            @Result(id = true, column = "id", property = "id")
    })
    User selectById(Integer id);

    /**
     * 用户名查找用户信息
     *
     * @param username
     * @return
     */
    @Select("select * from `user` where username = #{username} ")
    User selectByUsername(String username);

    /**
     * 根据用户id查询当前用户的所有文章
     *
     * @param id
     * @return
     */
    @Select("select * from user where id = #{id} ")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "articleList",
                    many = @Many(select = "com.lingnan.blog.mapper.ArticleMapper.selectByUid"))
    })
    User selectArticlesById(Integer id);

    /**
     * 插入用户信息
     *
     * @param user
     * @return 影响的行数
     */
    @InsertProvider(type = UserProvider.class, method = "insert")
    int insert(User user);

    /**
     * 更新用户信息
     *
     * @param user
     * @return 影响的行数
     */
    @UpdateProvider(type = UserProvider.class, method = "update")
    int update(User user);

    @Select("select id,nickname,photo,email,intro from `user` where role=#{user_role} ")
    User selectByUserRole(String user_role);
}